import React, { useEffect } from 'react'
import { useNavigate, useLocation } from 'react-router-dom'

function WithRouter(Cpn: React.ComponentType) {
    function NewCpn() {
        const navigate = useNavigate()
        const { pathname } = useLocation()
        const user = JSON.parse(localStorage.getItem('username') as string)
        useEffect(() => {
            if (!user &&  !["/login", "/case", "/home", "/history", "/my", "/shop",'/'].includes(pathname)) {
                navigate('/login')
            }
        }, [pathname])
        return <Cpn></Cpn>
    }
    return NewCpn
}

export default WithRouter
